package gamagora.game.entity
{
	import Box2D.Dynamics.Controllers.b2BuoyancyController;
	import Box2D.Dynamics.b2Body;
	
	import gamagora.game.entity.OSSound;
	import gamagora.game.resources.OSImages;
	import gamagora.game.resources.OSSounds;
	
	import org.flixel.FlxSprite;

	public class OSWaterBridge extends OSEntity
	{
		private static const CRAFT_WIDTH:Number = 196;
		private static const CRAFT_HEIGHT:Number = 25;
		private static const SPACE:Number = 0;
		
		private var _controller:b2BuoyancyController;
		private var _nbCrafts:uint;
		private var _drawWater:Boolean;
		private var _waterSprite:FlxSprite;
		private var _waterHeight:Number;
		private var _addController:Boolean;
		private var _sound:OSSound;
		
		public function OSWaterBridge(X:Number=0, Y:Number=0, nbCrafts:uint = 3, drawWater:Boolean = false, waterHeight:Number = 60)
		{
			super(BRIDGE, X, Y);
			_hide = true;
			
			_nbCrafts = nbCrafts;
			_drawWater = drawWater;
			_waterHeight = waterHeight;
			
			_sound = new OSSound(x, y, 0.1);
			_sound.loadEmbedded(OSSounds.WATER, true);
			add(_sound);
		}
		
		public override function addToWorld():void
		{
			_inWorld = true;
			for(var i:uint=0;i<_nbEntities;i++)
			{
				var entity:OSEntity = _entities[i] as OSEntity;
				if(!entity.inWorld)
				{
					addEntityToWorld(entity);
					
					if((_entities[i] as OSPhysicEntity) != null)
					{
						_controller.AddBody((_entities[i] as OSPhysicEntity).body);
					}
				}
			}
			
			if(!_addController)
			{
				_world.physicWorld.AddController(_controller);
				_addController = true;
			}
			//_world.physicWorld.AddController(_controller);
			
			_sound.x = x;
			_sound.y = y;
		}
		
		public override function removeFromWorld():void
		{
			_inWorld = false;
			for(var i:uint=0;i<_nbEntities;i++)
			{
				var entity:OSEntity = _entities[i] as OSEntity;
				if(entity.inWorld)
				{
					if((_entities[i] as OSPhysicEntity) != null)
					{
						_controller.RemoveBody((_entities[i] as OSPhysicEntity).body);
					}
					removeEntityFromWorld(entity);
				}
			}
			
			//_world.physicWorld.RemoveController(_controller);
		}
		
		public override function draw():void
		{
			super.draw();
			
			if(_drawWater)
			{
				_waterSprite.draw();
			}
		}
		
		public override function setStartX(decalX:Number):void
		{
			x += decalX;
			_startX += decalX;
			
			_controller = new b2BuoyancyController();
			_controller.normal.Set(0, -1);
			_controller.offset = -y / OSPhysicEntity.RATIO;
			_controller.density = 4;
			_controller.linearDrag = 5;
			_controller.angularDrag = 2;
			
			for(var i:uint=0;i<_nbCrafts;i++)
			{
				var box:OSBox = new OSBox(BRIDGE, x + i * (CRAFT_WIDTH + SPACE), y, CRAFT_WIDTH, CRAFT_HEIGHT, CRAFT_WIDTH, CRAFT_HEIGHT, OSImages.CRAFT);
				box.offset.y = 14;
				box.bodyDef.type = b2Body.b2_dynamicBody;
				box.fixDef.density = 2;
				box.fixDef.friction = 0.3;
				box.fixDef.restitution = 0.1;
				add(box);
			}
			
			if(_drawWater)
			{
				_waterSprite = new FlxSprite(x, y + 5);
				_waterSprite.makeGraphic(_nbCrafts * (200 + SPACE), _waterHeight - 5, 0xff000000);
			}
		}
		
		public override function destroy():void
		{
			super.destroy();
			if(_drawWater)
			{
				_waterSprite.destroy();
			}
		}
	}
}
